package service

import (
	"errors"
	cResp "gin/cdai/response"
	"gin/cdai/task"
	"gin/common"
	Mysql "gin/database"
	"gin/model"
	"gin/validate"
	uuid "github.com/satori/go.uuid"
	"github.com/spf13/viper"
	"os"
	"strings"
)

// 验证文件md5是否存在
func CheckFileMd5(MD5 string) (check bool, mediaData model.Media) {
	var mediaModel model.Media
	var err error

	mediaData, err = mediaModel.GetMediaDataByMD5(MD5)
	if err != nil {
		check = false
	} else {
		check = true
	}

	return
}

// 保存媒体数据
func CreateMedia(Data validate.AddMedia) (id uint64, err error) {
	var mediaModel model.Media
	mediaModel.Md5Name = Data.Md5Name
	mediaModel.Md5 = Data.Md5
	mediaModel.Type = Data.Type
	mediaModel.Status = Data.Status
	mediaModel.Param = Data.Param
	mediaModel.RelativePath = Data.RelativePath

	// 填充UUID
	mediaModel.Uuid = uuid.NewV4().String()

	return mediaModel.Insert()

}

// 更新文件状态通过 Md5Name
func UpdateStatusByMd5Name(Md5Name string, Status string) (err error) {
	var mediaModel model.Media
	err = mediaModel.UpdateStatusByMd5Name(Md5Name, Status)
	return
}

// 通过Md5 Name 查找媒体
func SelectMediaByMd5Name(Md5Name string) (mediaData []model.Media, err error) {
	var mediaModel model.Media
	mediaData, err = mediaModel.SelectMediaByMd5Name(strings.Split(Md5Name, ","))
	return
}

// 更新文件状态通过 Md5Name
func RemoveMediaByMd5Name(Md5Name string) (err error) {
	var mediaModel model.Media

	// 1.先查找当前存在的文件
	mediaData, err := mediaModel.SelectMediaByMd5Name(strings.Split(Md5Name, ","))

	if err != nil {
		return
	}

	// 删除存在数据的文件夹
	basePath := viper.GetString("server.staticPath")
	for _, mediaItem := range mediaData {
		err = os.RemoveAll(basePath + mediaItem.Md5Name)
		if err != nil {
			break
		}
	}

	if err != nil {
		return
	}

	// 清除数据库数据
	err = mediaModel.RemoveMediaByMd5Name(strings.Split(Md5Name, ","))
	return
}

func FindMedia(Param common.Filter) (resData model.Media, err error) {
	var logModel model.Media
	resData, err = logModel.Find(Param)
	return
}

func CreateMediaAnalysisTask(mediaId uint64) (model.Media, error) {
	media := model.Media{}
	result := Mysql.DB.Model(&model.Media{}).Where("id = ?", mediaId).Take(&media)
	if result.Error != nil {
		return media, result.Error
	}
	shareFoldName := viper.GetString("cdai.share_folder_name")
	fileName := strings.Replace(media.RelativePath, "static", shareFoldName, 1)
	callBackUrl := viper.GetString("cdai.call_back_url")

	var resp *cResp.CreateTaskResp
	var err error

	if media.Type == "audio" {
		resp, err = task.CreateVoiceTask(fileName, callBackUrl)
	} else if media.Type == "video" {
		resp, err = task.CreateFaceTask(fileName, callBackUrl)
	} else {
		return media, errors.New("not support media type" + media.Type)
	}

	if err != nil {
		return media, err
	}
	media.TaskUUID = *resp.Rows.TaskUuid
	result = Mysql.DB.Model(&media).Updates(&media)
	if result.Error != nil {
		return media, result.Error

	}
	return media, nil
}

// 通过Md5 Name 查找媒体
func FindMediaByMd5(Md5 string) (mediaData model.Media, err error) {
	var mediaModel model.Media
	mediaData, err = mediaModel.FindMediaByMd5Name(Md5)
	return
}
